//
//  WBNavigationViewController.swift
//  IOS_Mock_WB
//
//  Created by edy on 2024/9/19.
//

import UIKit

class WBNavigationViewController: UINavigationController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        // 隐藏默认的Bar
        navigationBar.isHidden = true
    }
    // 重写push方法
    // viewController是被push的Controller
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        // push时候隐藏tabbar底部导航栏
        // 如果不是栈底控制器才需要隐藏，否则不处理
        if children.count > 0{
            viewController.hidesBottomBarWhenPushed=true
            
            
            // 判断viewController是否为基类
            if let vc = viewController as? WBBaseViewController {
                
                var title = "返回"
                
                //push的子控制器个数
                if children.count == 1{
                    title = children.first?.title ?? "返回"
                }
                
                // 取出自定义的navItem
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popToParent),isBack: true)
            }
        }
         
        
        
        super.pushViewController(viewController, animated: animated)
    }
    
    @objc private func popToParent(){
        
        // 返回上一级目录
        popViewController(animated: false)
        
    }
    
}
